﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.IO;
using LeagueOfRila.Controller;
using LeagueOfRila.View;
using Data.Maps;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace LeagueOfRila
{
    public static class MapBlocksEditor
    {
        public static Block currentBlock = new Block();
        public static int currentBlockState = 0;
        public static StreamWriter mapBlocksFile;
        public static bool mapBlocksFileInitialized = false;
        public static string filePath = "./mapblocks.txt";
        public static string fileContent = "";

        public static bool IsMapBlocksEditorActivated(bool init = true)
        {
            if (ConfigurationSettings.AppSettings["MapBlocksEditor"].ToLower() == true.ToString().ToLower())
            {
                if (init && !mapBlocksFileInitialized)
                {
                    mapBlocksFile = File.CreateText(filePath);
                    mapBlocksFileInitialized = true;
                }
                return true;
            }
            else
                return false;
        }

        public static bool SaveMapBlock()
        {
            string codeBlock = string.Format("listblock.Add(new Block({0}, {1}, {2}, {3}));\n", currentBlock.posX.ToString(), currentBlock.posY.ToString(), currentBlock.width.ToString(), currentBlock.height.ToString());
            fileContent = string.Concat(fileContent, codeBlock);
            try
            {
                mapBlocksFile.WriteLine(fileContent);
                mapBlocksFile.Close();
                
                return true;
            }
            catch(Exception e)
            {
                return false;
            } 
        }

        public static bool ClearMapBlock()
        {
            currentBlockState = 0;
            currentBlock = new Block(); 
            return true;
        }

        public static void CheckMapBlocksEditor(List<Keys> previousKeys, List<Keys> currentKeys, GameController controller)
        {
            if(IsMapBlocksEditorActivated())
            {


                GameView view = controller.GetModel().GetView();

                //touche R => création d'un block
                //1 R = position en haut à gauche
                //2 R = width et height du block
                //3 R = Save du block
                //touche C => annulation
                List<Keys> listprevious = previousKeys;
                foreach (Keys key in listprevious)
                {
                    if (!currentKeys.Contains(key))
                    {
                        if (key.ToString().ToLower() == "r")
                        {
                            if (currentBlockState == 0)
                            {
                                currentBlock = new Block();
                                currentBlock.posX = Client.player.posX + (int)view.mapOrigin.X;
                                currentBlock.posY = Client.player.posY + (int)view.mapOrigin.Y;
                                currentBlockState++;
                            }
                            else if (currentBlockState == 1)
                            {
                                currentBlock.width = currentBlock.posX - (Client.player.posX + (int)view.mapOrigin.X);
                                currentBlock.height = currentBlock.posY - (Client.player.posY + (int)view.mapOrigin.Y);
                                currentBlockState++;
                            }
                            else if (currentBlockState == 2)
                            {
                                controller.GetModel().currentMap.listblock.Add(currentBlock);
                                SaveMapBlock();
                                ClearMapBlock();
                            }
                        }
                        else if (key.ToString().ToLower() == "c")
                        {
                            ClearMapBlock();
                        }
                    }
                }                 
            }
        }

    }
}
